<template>
  <el-dialog title="新增权限" :visible.sync="showDialog" @close="onCancel">
    <el-form ref="formRef" :rules="rules" label-width="120px" :model="formModel">
      <el-form-item prop="name" label="权限名称">
        <el-input v-model="formModel.name" style="width: 85%" />
      </el-form-item>
      <el-form-item prop="code" label="权限标识">
        <el-input v-model="formModel.code" style="width: 85%" />
      </el-form-item>
      <el-form-item prop="description" label="权限描述">
        <el-input v-model="formModel.description" style="width: 85%" type="textarea" :rows="3" />
      </el-form-item>
      <el-form-item prop="enVisible" label="开启">
        <el-switch
          v-model="formModel.enVisible"
          active-value="1"
          inactive-value="0"
        />
      </el-form-item>
    </el-form>
    <el-row type="flex" justify="center">
      <el-button size="mini" type="primary" @click="onSubmit">确认</el-button>
      <el-button size="mini" @click="onCancel">取消</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { permissionAddService, permissionListService, permissionDetailService, permissionUpdateService } from '@/api/permission'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentNodeId: {
      // 获取当前所编辑的节点id
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      formModel: {
        name: '',
        code: '',
        description: '',
        type: 1,
        pid: 0,
        enVisible: '1'
      },
      rules: {
        name: [{ required: true, message: '请输入权限名称', trigger: 'blur' },
          {
            // 不能与子节点重名
            validator: async(rule, value, callback) => {
              let res = await permissionListService()
              // console.log(res)
              // console.log(this.formModel.pid)
              // 编辑权限时，排除当前权限名称
              if (this.formModel.id) {
                res = res.filter(item => item.id !== this.formModel.id)
              }
              res.forEach(item => {
                if (item.pid === this.formModel.pid && item.name === value) {
                  callback(new Error('权限名称已存在于当前父级下'))
                }
              })
              callback()
            },
            trigger: 'blur'
          }
        ],
        code: [{ required: true, message: '请输入权限标识', trigger: 'blur' },
          {
            // 不能与所有权限标识重名
            validator: async(rule, value, callback) => {
              let res = await permissionListService()
              // 编辑权限时，排除当前权限标识
              if (this.formModel.id) {
                res = res.filter(item => item.id !== this.formModel.id)
              }
              res.forEach(item => {
                if (item.code === value) {
                  callback(new Error('权限标识已存在'))
                }
              })
              callback()
            },
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    onCancel() {
      // 重置表单,由于resetFields只会重置模板中原先就有的值，不会重置传入模板的值
      // 因此需要重置formModel的值
      this.formModel = {
        name: '',
        code: '',
        description: '',
        type: 1,
        pid: 0,
        enVisible: '1'
      }
      this.$emit('update:showDialog', false)
      this.$refs.formRef.resetFields()
    },
    onSubmit() {
      this.$refs.formRef.validate(async valid => {
        if (valid) {
          if (this.formModel.id) {
            // 修改权限
            await permissionUpdateService(this.formModel)
            this.$message.success('修改权限成功')
          } else {
            // 新增权限
            if (this.currentNodeId === 0) {
              // 如果当前节点为根节点，则pid为0
              this.formModel.pid = 0
            } else {
              // 如果当前节点为子节点，则pid为当前节点的id
              this.formModel.pid = this.currentNodeId
              this.formModel.type = 2 // 修改权限为二级权限
            }
            // this.formModel.pid = this.currentNodeId === 0 ? 0 : this.currentNodeId
            await permissionAddService({ ...this.formModel, pid: this.currentNodeId })
            this.$message.success('添加权限成功')
          }
          this.$emit('success')
        }
      })
    },
    async getPermissionDetail() {
      this.formModel = await permissionDetailService(this.currentNodeId)
      // console.log(this.formModel) // 获取到当前节点的详细信息，这里只有修改时才会有formModel.id
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
